package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.ruoyi.common.utils.IDGenerator;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.dao.TCodeDao;
import com.ruoyi.system.domain.dto.TCodeDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.TCodeVo;
import com.ruoyi.system.mapper.TCodeMapper;
import com.ruoyi.system.mapper.TCodeTagMapper;
import com.ruoyi.system.mapper.TTagMapper;
import com.ruoyi.system.service.TCodeServier;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.IdGenerator;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TCodeServierImpl implements TCodeServier {

    @Resource
    private TCodeMapper tCodeMapper;
    @Resource
    private TTagMapper tTagMapper;
    @Resource
    private TCodeTagMapper tCodeTagMapper;
    @Resource
    private TCodeDao tCodeDao;

    @Override
    public Integer insertCode(TCodeDto.Insert insert) {
        TCode tCode = BeanUtil.copyProperties(insert, TCode.class);
        tCode.setId(IdUtil.getSnowflake().nextId());
        tCode.setDelFlag((byte) 0);
        tCode.setCreateBy(SecurityUtils.getUsername());
        tCode.setCreateTime(new Date());
        tCode.setPinyinSimple(PinyinUtil.getFirstLetter(insert.getText(), ""));
        tCode.setPinyinFull(PinyinUtil.getPinyin(insert.getText(), ""));
        //新增标签关联
        List<Long> tagIdList = insert.getTagIdList();
        if (CollectionUtil.isNotEmpty(tagIdList)) {
            for (Long aLong : tagIdList) {
                TCodeTag tCodeTag = new TCodeTag();
                tCodeTag.setTagId(aLong);
                tCodeTag.setCodeId(tCode.getId());
                tCodeTagMapper.insertSelective(tCodeTag);
            }
        }
        return tCodeMapper.insertSelective(tCode);
    }

    @Override
    public Integer updateCode(TCodeDto.Update update) {
        TCode tCode = BeanUtil.copyProperties(update, TCode.class);
        tCode.setUpdateBy(SecurityUtils.getUsername());
        tCode.setUpdateTime(new Date());
        List<Long> tagIdList = update.getTagIdList();
        if (CollectionUtil.isNotEmpty(tagIdList)) {
            //删除旧关联
            TCodeTagExample tCodeTagExample = new TCodeTagExample();
            tCodeTagExample.createCriteria().andCodeIdEqualTo(update.getId());
            tCodeTagMapper.deleteByExample(tCodeTagExample);
            //新增标签关联
            for (Long aLong : tagIdList) {
                TCodeTag tCodeTag = new TCodeTag();
                tCodeTag.setTagId(aLong);
                tCodeTag.setCodeId(tCode.getId());
                tCodeTagMapper.insertSelective(tCodeTag);
            }
        }
        return tCodeMapper.updateByPrimaryKeySelective(tCode);
    }

    @Override
    public Integer deleteCode(TCodeDto.Delete delete) {
        TCodeExample tCodeExample = new TCodeExample();
        tCodeExample.createCriteria().andIdIn(delete.getIdList());
        TCode tCode = new TCode();
        tCode.setDelFlag((byte) 1);
        return tCodeMapper.updateByExampleSelective(tCode, tCodeExample);
    }

    @Override
    public List<TCodeVo.Result> listCode(TCodeDto.Query list) {
        return tCodeDao.query(list.getCode(), list.getText(), list.getTagIdList());
    }

    @Override
    public void exportCode(TCodeDto.Export export) {

    }

    @Override
    public Integer insertTag(TCodeDto.TagInsert tagInsert) {
        TTag tTag = BeanUtil.copyProperties(tagInsert, TTag.class);
        tTag.setId(IdUtil.getSnowflake().nextId());
        tTag.setCreateBy(SecurityUtils.getUsername());
        tTag.setCreateTime(new Date());
        return tTagMapper.insertSelective(tTag);
    }

    @Override
    public Integer updateTag(TCodeDto.TagUpdate tagUpdate) {
        TTag tTag = BeanUtil.copyProperties(tagUpdate, TTag.class);
        tTag.setUpdateBy(SecurityUtils.getUsername());
        tTag.setUpdateTime(new Date());
        return tTagMapper.updateByPrimaryKeySelective(tTag);
    }

    @Override
    public Integer deleteTag(TCodeDto.TagDelete tagDelete) {
        TTagExample tTagExample = new TTagExample();
        tTagExample.createCriteria().andIdIn(tagDelete.getIdList());
        TTag tTag = new TTag();
        tTag.setDelFlag((byte) 1);
        return tTagMapper.updateByExampleSelective(tTag, tTagExample);
    }

    @Override
    public List<TTag> listTag(TCodeDto.QueryTag queryTag) {
        TTagExample tTagExample = new TTagExample();
        TTagExample.Criteria criteria = tTagExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotEmpty(queryTag.getName())) {
            criteria.andNameLike("%" + queryTag.getName() + "%");
        }
        tTagExample.setOrderByClause("create_time desc");
        return tTagMapper.selectByExample(tTagExample);
    }

    @Override
    public void importCode(List<TCodeDto.Import> list) {
        for (TCodeDto.Import importCode : list) {
            String model = importCode.getModel();
            TTagExample tTagExample = new TTagExample();
            tTagExample.createCriteria().andNameEqualTo(model);
            List<TTag> tTags = tTagMapper.selectByExample(tTagExample);
            TTag tTag = new TTag();
            if (CollectionUtil.isEmpty(tTags)) {
                tTag.setId(IdUtil.getSnowflake().nextId());
                tTag.setName(model);
                tTagMapper.insertSelective(tTag);
            }else {
                tTag = tTags.get(0);
            }

            TCode tCode = new TCode();
            tCode.setId(IdUtil.getSnowflake().nextId());
            tCode.setCode(importCode.getCode());
            tCode.setText(importCode.getText());

            tCode.setPinyinSimple(PinyinUtil.getFirstLetter(importCode.getText(), ""));
            tCode.setPinyinFull(PinyinUtil.getPinyin(importCode.getText(), ""));
            tCodeMapper.insertSelective(tCode);

            TCodeTag tCodeTag = new TCodeTag();
            tCodeTag.setCodeId(tCode.getId());
            tCodeTag.setTagId(tTag.getId());
            tCodeTagMapper.insertSelective(tCodeTag);
        }
    }
}
